package com.jnshu.controller;

import com.jnshu.pojo.ZuulManageDto;
import com.jnshu.service.AdminService;
import com.jnshu.tools.resultUtils.RUtil;
import com.jnshu.tools.resultUtils.ResultBean;
import com.jnshu.tools.resultUtils.ResultEnum;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author pipiretrak
 * @date 2019/8/3 - 17:02
 */
@Log
@RestController
public class LoginController {

    @Autowired(required = false)
    AdminService adminService;

    @RequestMapping(value = "/b/login")
    public ResultBean loginAdmin (){
        return RUtil.ok("请登录");
    }

    @RequestMapping(value = "/b/login/success")
    public ResultBean loginSuccess(){
        log.info("成功登录管理账号+++++++++++++++++++++++++++++++++++++++++++++++++");
        String name = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        log.info("输入的姓名是："+name);
        ZuulManageDto loginDto = adminService.selectLoginInfo(name);
        log.info("返回给前端的数据为："+loginDto);
        return RUtil.ok(loginDto);
    }

    @GetMapping("/b/login/error")
    public ResultBean loginError(HttpServletRequest request, HttpServletResponse response){
        response.setContentType("text/html;charset=utf-8");
        AuthenticationException exception = (AuthenticationException) request.getSession().getAttribute("SPRING_SECURITY_LAST_EXCEPTION");
        log.info("登录失败，错误信息为："+exception.toString());
        return RUtil.error(ResultEnum.FAILURE);
    }

    @GetMapping("/logoutSuccess")
    public ResultBean logoutSuccess(){
        return RUtil.ok("已成功登出");
    }
}
